package com.crb.oauth2.api.wechat.cp.controller;

import com.crb.oauth2.api.wechat.cp.config.WxCpConfiguration;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * jsapi 演示接口的 controller.
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 * @date 2020-04-25
 */
@AllArgsConstructor
@Controller
@RequestMapping("/wx/jsapi")
public class WxJsapiController {

    @GetMapping("/share/{agentId}")
    public String share(@PathVariable Integer agentId, ModelMap map) {
        map.put("agentId", agentId);
        return "share";
    }

    @ResponseBody
    @GetMapping("/createJsapiSignature/{agentId}")
    public WxJsapiSignature createJsapiSignature(@PathVariable Integer agentId,@RequestParam("url") String url) throws WxErrorException {
        WxCpService cpService = WxCpConfiguration.getCpService(agentId);
        WxJsapiSignature jsapiSignature = cpService.createJsapiSignature(url);
        return jsapiSignature;
    }
}
